<?php

namespace App\Http\Middleware;
use Illuminate\Contracts\Encryption\DecryptException;
//use Illuminate\Support\Facades\Cache;

use App\Models\User;
use Crypt,Log;
use Closure;

class WxAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next){
    	
		$user_token = request('user_token');
		try {
		    $res = Crypt::decrypt($user_token);
			
			$user = User::where('uid',$res['uid'])->where('token',$res['user_token'])->select('uid','token')->first();
			if(!$user){
				return redirect()->route('refuse');
			}
			$request->uid = $res['uid'];
		} catch (DecryptException $e) {
			return redirect()->route('refuse');
		}
        return $next($request);
    }
}
